package com.skyun.watchmanager.wear;

import java.io.File;
import java.lang.reflect.Method;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.content.res.Resources.NotFoundException;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class ColorActivity extends Activity implements ReadColor.IListener {
    private Button btn_start;
    private TextView txt_pk;
    private TextView txt_color;
    private Handler mHandler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color);
        btn_start = (Button) findViewById(R.id.btn_start);
        txt_pk = (TextView) findViewById(R.id.txt_pk);
        txt_color = (TextView) findViewById(R.id.txt_color);
        btn_start.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                new ReadColor().doRead(ColorActivity.this, ColorActivity.this, R.color.test);
            }
        });
    }

    @Override
    public void onReadPkName(final String pk_name) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                txt_pk.setText(pk_name);
            }
        });
    }

    @Override
    public void onReadColor(final int color) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                txt_color.setText(color + "");
                btn_start.setBackgroundColor(color);
            }
        });
    }
}

class ReadColor {
    public static ExecutorService pool = Executors.newFixedThreadPool(1);
    private static String SDCardPath = Environment
            .getExternalStorageDirectory().getPath();


    public interface IListener {
        public void onReadPkName(String pk_name);


        public void onReadColor(int color);
    }

    public void doRead(final Context context, final IListener listener,
                       final int color_id) {


        pool.execute(new Runnable() {
            @Override
            public void run() {
// 制作text.thinking压缩文件，文件目录与本apk相同
                File file = new File(SDCardPath + "/" + "text.thinking");
// 获取text.thinking的packageName
                PackageManager mPm = context.getPackageManager();
                PackageInfo mInfo = mPm.getPackageArchiveInfo(file.getAbsolutePath(), PackageManager.GET_ACTIVITIES);
                String pk_name = mInfo.packageName;
                listener.onReadPkName(pk_name);// 需要READ_EXTERNAL_STORAGE权限
// 通过反射获取text.thinking的参数
                AssetManager assetManager = null;
                try {
                    assetManager = AssetManager.class.newInstance();
                    Method addAssetPath = assetManager.getClass().getMethod("addAssetPath", String.class);
                    addAssetPath.invoke(assetManager, file.getAbsolutePath());
                } catch (Exception e) {
                    e.printStackTrace();
                }
// 用本地Resources的参数（屏幕、配置信息等）实例化
                DisplayMetrics disp = context.getResources().getDisplayMetrics();
                Configuration config = context.getResources().getConfiguration();
                Resources newResource = new Resources(assetManager, disp, config);
                listener.onReadColor(doReadColor(context, listener, newResource, color_id, pk_name));
            }
        });


    }

    private int doReadColor(Context context, IListener listener,
                            Resources newResource, int color_id, String pk_name) {
        int originColor = context.getResources().getColor(color_id);
// 如果读取Resource失败，则用原有的Resource
        if (newResource == null) {
            return originColor;
        }
// 找到本地资源id对应的资源name
        String resName = context.getResources().getResourceEntryName(color_id);
// 用资源name找到在外界Resource里的资源id
        int trueResId = newResource.getIdentifier(resName, "color", pk_name);
        int trueColor = 0;
        try {
            trueColor = newResource.getColor(trueResId);
        } catch (NotFoundException e) {
            e.printStackTrace();
            trueColor = originColor;
        }
        return trueColor;
    }
}